using FPLibrary;
using Pvp.Ecs;
using UnityEngine;

namespace Pvp
{
    /// <summary>
    /// 门开关触发逻辑：当前stage下所有可以看到的怪物都死掉 && 门在玩家视野范围内时 =》门才打开
    /// </summary>
    [EntityDeclare((int)EntityType.gear, (int) GearType.门机关1)]
    class EntityDeclare1641 :BaseEntityDeclare
    {
        private IView m_View;
        private IStage m_Stage;
        
        public override void OnEntityCreate(Entity entity,object userData)
        {
            Debug.LogError("0000000000000");
            
            m_View = entity.AddComponent<IView>();
            entity.AddComponent<IUnit>();
            var door =  entity.AddComponent<IGearUnit>();
            door.GearLogicTicker = OnTicker;

            m_Stage = Entities.GetSingleComponent<IStage>();
        }

        private void OnTicker(IGearUnit gear,int frameIndex)
        {
            //判定当前Stage下所有怪物是否被消灭
            //如果完全被消灭，则 开关触发 true，否则 false
            gear.IsOpened = m_Stage.IsMonsterCleared() && m_View.IsInView;
        }
    }
    
    [EntityDeclare((int)EntityType.gear, (int) GearType.地刺机关1)]
    class EntityDeclare1711 :BaseEntityDeclare
    {
        private IUnit m_Unit;
        private IView m_View;
        private IStage m_Stage;

        private int flag = 1;
        
        public override void OnEntityCreate(Entity entity,object userData)
        {
            m_View = entity.AddComponent<IView>();
            m_Unit = entity.AddComponent<IUnit>();
            var gear =  entity.AddComponent<IGearUnit>();
            gear.GearLogicTicker = OnTicker;
            gear.IsOpened = true;

            m_Stage = Entities.GetSingleComponent<IStage>();
        }

        private void OnTicker(IGearUnit gear,int frameIndex)
        {
            if (frameIndex % 120 == 0)
            {
                m_Unit.Position += FPVector.up * 3 * flag;
                flag *= -1;
            }
        }

        public void Clone()
        {
            
        }
    }
    
    [EntityDeclare((int)EntityType.gear, (int) GearType.地刺机关2)]
    class EntityDeclare1712 :BaseEntityDeclare
    {
        private IUnit m_Unit;
        private IView m_View;
        private IStage m_Stage;

        private int flag = 1;
        
        public override void OnEntityCreate(Entity entity,object userData)
        {
            m_View = entity.AddComponent<IView>();
            m_Unit = entity.AddComponent<IUnit>();
            var gear =  entity.AddComponent<IGearUnit>();
            gear.GearLogicTicker = OnTicker;
            gear.IsOpened = true;

            m_Stage = Entities.GetSingleComponent<IStage>();
        }

        private void OnTicker(IGearUnit gear,int frameIndex)
        {
            if (frameIndex % 30 == 0)
            {
                m_Unit.Position += FPVector.left * 3 * flag;
                flag *= -1;
            }
        }
    }
    
    [EntityDeclare((int)EntityType.gear, (int) GearType.地刺机关3)]
    class EntityDeclare1713 :BaseEntityDeclare
    {
        private IUnit m_Unit;
        private IView m_View;
        private IStage m_Stage;

        public override void OnEntityCreate(Entity entity,object userData)
        {
            m_View = entity.AddComponent<IView>();
            m_Unit = entity.AddComponent<IUnit>();
            var gear =  entity.AddComponent<IGearUnit>();
            gear.GearLogicTicker = OnTicker;
            gear.IsOpened = true;

            m_Stage = Entities.GetSingleComponent<IStage>();
        }

        private void OnTicker(IGearUnit gear,int frameIndex)
        {
            if (frameIndex % 3 == 0)
            {
                m_Unit.Rotation += FPVector.up * 2f;
            }
        }
    }
}